package com.zhuoshi.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.zhuoshi.bean.Person;
import com.zhuoshi.utils.SQLUtils;

public interface PersonDao {
	
	@Select("select * from person where p_id = #{id}")
	@Results(value={
			@Result(property="pid",column="p_id",id=true),
			@Result(property="pname",column="p_name"),
			@Result(property="age",column="p_age")	
	})
	public Person selectByPid(Integer pid);
	
	
	
	@Select("select * from person")
	@Results(value={
			@Result(property="pid",column="p_id",id=true),
			@Result(property="pname",column="p_name"),
			@Result(property="age",column="p_age")	
	})
	public List<Person> selectAll();
	
	
	@Select("select * from person where p_name like '%${name}%'")
	@Results(value={
			@Result(property="pid",column="p_id",id=true),
			@Result(property="pname",column="p_name"),
			@Result(property="age",column="p_age")	
	})
	public List<Person> selectByNameLike(Map<String,Object> map);
	
	/**
	 * 使用 动态条件查询   依赖查询类(条件拼接)
	 * @param map
	 * @return
	 */
	@SelectProvider(type=SQLUtils.class,method="util")
	@Results(value={
			@Result(property="pid",column="p_id",id=true),
			@Result(property="pname",column="p_name"),
			@Result(property="age",column="p_age")	
	})
	public List<Person> selectByMap(Map<String,Object> map);
	
	@Insert("insert into person(p_name,p_age) values (#{pname},#{age})")
	public void insert(Person person);
	
	@Update("update person set p_name=#{pname},p_age=#{age} where p_id = #{pid}")
	public void update(Person person);
	
	@Delete("delete from person where p_id = #{id}")
	public void delete(Integer pid);
	
	//关联关系
	@Select("select * from person p,orders o where p.p_id = o.p_id and p.p_id = #{id}")
	@ResultMap("com.zhuoshi.personMapper.oneToManyMap")
	public Person selectPersonAndOrders(Integer pid);
}
